package com.zsc.wxapp.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsc.wxapp.entity.PowerGroup;
import com.zsc.wxapp.entity.dto.AnnouncementDTO;
import com.zsc.wxapp.entity.dto.PowerGroupDTO;
import com.zsc.wxapp.entity.vo.ManagerVO;
import com.zsc.wxapp.entity.vo.RoleVO;
import com.zsc.wxapp.mapper.PowerGroupMapper;
import com.zsc.wxapp.result.Result;
import com.zsc.wxapp.services.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/wxapp/admin")
@RestController("adminController")
//@Api(tags = "超级管理员后台相关接口")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private PowerGroupMapper powerGroupMapper;
    /**
     * 添加权限组
     * @param powerGroupDTO
     * @return
     */
    @PostMapping("/addPowerGroup")
    @ApiOperation(value = "添加权限组")

    public Result<?> addPowerGroup(PowerGroupDTO powerGroupDTO){
        try {
            adminService.addPowerGroup(powerGroupDTO);
        }catch (Exception e){
            log.error("添加权限组失败",e);
            return Result.error("添加权限组失败");
        }
        return Result.success();
    }

    /**
     * 获取权限组
     * @return
     */
    @GetMapping("/getPowerGroup")
    @ApiOperation(value = "获取权限组")
    public Result<List<PowerGroup>> getPowerGroup( ) {
        List<PowerGroup> powerGroups;
        try {
            powerGroups = adminService.getPowerGroup();
        } catch (Exception e) {
            log.error("获取权限组失败", e);
            return Result.error("获取权限组失败");
        }
        return Result.success(powerGroups);
    }

    /**
     * 删除权限组
     * @param ids
     * @return
     */
    @DeleteMapping("/deletePowerGroup")
    @ApiOperation(value = "删除权限组")
    public Result<?> deleteAnnouncement(@RequestParam("ids") List<Integer> ids){
        try {
            adminService.deletePowerGroup(ids);
        }catch (Exception e){
            log.error("删除权限组失败",e);
            return Result.error("删除权限组失败");
        }
        return Result.success("删除权限组成功");
    }

    /**
     * 获取对应权限用户
     * 1终端用户 2业务层用户 3管理员
     * @param roleId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getRole")
    @ApiOperation(value = "获取对应权限用户")
    public Result<PageInfo> getRole(Integer roleId,Integer pageNum,Integer pageSize){
        log.info("获取角色信息");
        PageHelper.startPage(pageNum,pageSize);
        List<RoleVO> roleVOS = adminService.getRole(roleId);
//        Integer i = 1;
//        for(RoleVO roleVO:roleVOS){
//            roleVO.setId(i);
//            i++;
//        }
        PageInfo pageInfo = new PageInfo(roleVOS);
        return Result.success(pageInfo);
    }

    /**
     * 修改用户状态
     * @param userId
     * @param isValid
     * @return
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改用户状态")
    public Result<?> updateStatus(Integer userId,Integer isValid){
        log.info("修改用户状态");
        powerGroupMapper.updateUserStatus(userId,isValid);
        return Result.success("修改用户状态成功");
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public Result<?> deleteUser(Integer userId){
        log.info("删除用户");
        powerGroupMapper.deleteUser(userId);
        return Result.success("删除用户成功");
    }

    /**
     * 修改用户权限组
     * @param userId
     * @param powerGroupId
     * @return
     */
    @PostMapping("/updateUserPowerGroup")
    @ApiOperation(value = "修改用户权限组")
    public Result<?> updateUserPowerGroup(Integer userId,Integer powerGroupId){
        log.info("修改用户权限组");
        powerGroupMapper.updateUserPowerGroup(userId,powerGroupId);
        return Result.success("修改用户权限组成功");
    }

    /**
     * 获取经理
     * @return
     */
    @GetMapping("getManager")
    @ApiOperation(value = "获取经理")
    public Result<PageInfo> getManager(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ManagerVO> managerVOS = adminService.getManager();
        PageInfo pageInfo = new PageInfo(managerVOS);
        return Result.success(pageInfo);
    }
}
